﻿using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using ERP_RORZE_SYSTEM;
using System.Data;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class rpt_F4Producing : DevExpress.XtraReports.UI.XtraReport
    {
        public string month;

        private DataTable ProducingTable;
        
        public rpt_F4Producing()
        {
            InitializeComponent();
            this.month = null;
            this.ProducingTable = new DataTable();
            this.ProducingTable.Columns.Add("CountNo", typeof(int));
            this.ProducingTable.Columns.Add("PartEnglishName", typeof(string));
            this.ProducingTable.Columns.Add("PartNo", typeof(string));
            this.ProducingTable.Columns.Add("ExportQty", typeof(float));
            this.ProducingTable.Columns.Add("FromSerial", typeof(string));
            this.ProducingTable.Columns.Add("ToSerial", typeof(string));
        }

        public void BindData()
        {
            this.monthLabel.Text = this.month.Substring(0, 4) + "/" + this.month.Substring(4, 2);
            this.noCount.DataBindings.Add("Text", DataSource, "CountNo");
            this.partName.DataBindings.Add("Text", DataSource, "PartEnglishName");
            this.partNo.DataBindings.Add("Text", DataSource, "PartNo");
            this.quantityText.DataBindings.Add("Text", DataSource, "ExportQty");
            this.serialFrom.DataBindings.Add("Text", DataSource, "FromSerial");
            this.serialTo.DataBindings.Add("Text", DataSource, "ToSerial");
        }

        public void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT row_number() OVER (ORDER BY t1.[CompID], t2.PartEnglishName, t1.[FromSerialNo]) [CountNo],"
                + " t1.[CompID] AS PartNo, t2.PartEnglishName, t1.[Quantity] AS ExportQty, t1.[FromSerialNo] AS FromSerial,"
                + " t1.[ToSerialNo] AS ToSerial"
                + " FROM [231_TimePlan] AS t1 LEFT JOIN"
                + " [222_Part] AS t2 ON t1.[CompID] = t2.PartNo"
                + " WHERE SUBSTRING(CONVERT(NVARCHAR(8), t1.[FinishedDate], 112), 1, 6) = '" + this.month + "'"
                + " AND t1.[FromSerialNo] IS NOT NULL";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            
            if (sourceDataSet != null)
            {
                try
                {                    
                    int countIndex = 1;
                    List<int> collected = new List<int>();
                    foreach (DataRow producing_row in sourceDataSet.Tables[0].Rows)
                    {
                        if (!collected.Contains(Int32.Parse(producing_row["CountNo"].ToString())))
                        {
                            collected.Add(Int32.Parse(producing_row["CountNo"].ToString()));

                            string fromserial = producing_row["FromSerial"].ToString().Trim();
                            string toserial = producing_row["ToSerial"].ToString().Trim();
                            float quantity = float.Parse(producing_row["ExportQty"].ToString());
                            
                            bool next_step = true;
                            while (next_step)
                            {
                                next_step = false;
                                DataRow[] selected_rows = sourceDataSet.Tables[0].Select("PartNo = '" + producing_row["PartNo"].ToString().Trim() + "'");
                                if (selected_rows.Length > 0)
                                {
                                    foreach (DataRow selected_row in selected_rows)
                                    {
                                        if (selected_row["FromSerial"].ToString().Trim() != fromserial)
                                        {
                                            if (!collected.Contains(Int32.Parse(selected_row["CountNo"].ToString())))
                                            {
                                                if (CheckContinuous(toserial, selected_row["FromSerial"].ToString().Trim()))
                                                {
                                                    toserial = selected_row["ToSerial"].ToString().Trim();
                                                    quantity += float.Parse(selected_row["ExportQty"].ToString());

                                                    collected.Add(Int32.Parse(selected_row["CountNo"].ToString()));
                                                    next_step = true;
                                                }
                                                else
                                                {
                                                    if (CheckContinuous(selected_row["ToSerial"].ToString().Trim(), fromserial))
                                                    {
                                                        fromserial = selected_row["FromSerial"].ToString().Trim();
                                                        quantity += float.Parse(selected_row["ExportQty"].ToString());

                                                        collected.Add(Int32.Parse(selected_row["CountNo"].ToString()));
                                                        next_step = true;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                                                       
                            DataRow result_row = this.ProducingTable.NewRow();
                            result_row["CountNo"] = countIndex;
                            result_row["PartNo"] = producing_row["PartNo"].ToString().Trim();
                            result_row["PartEnglishName"] = producing_row["PartEnglishName"].ToString().Trim();
                            result_row["ExportQty"] = quantity;
                            result_row["FromSerial"] = fromserial;
                            result_row["ToSerial"] = toserial;
                            this.ProducingTable.Rows.Add(result_row);
                            countIndex++;
                        }
                    }

                    //this.DataSource = sourceDataSet.Tables[0];
                    this.DataSource = this.ProducingTable;
                    BindData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Cannot Load data to report");
            }
        }

        private bool CheckContinuous(string previous_serial, string nex_serial)
        {            
            bool ret = false;
            if (previous_serial.Length == nex_serial.Length)
            {
                if (previous_serial.Substring(0, 4) == nex_serial.Substring(0, 4))
                {
                    if (((previous_serial[previous_serial.Length - 1] == 'F') && (nex_serial[nex_serial.Length - 1] == 'F')) ||
                        ((previous_serial[previous_serial.Length - 1] == 'f') && (nex_serial[nex_serial.Length - 1] == 'f')))
                    {
                        int previous = Int32.Parse(previous_serial.Substring(previous_serial.Length - 4, 3));
                        int next = Int32.Parse(nex_serial.Substring(nex_serial.Length - 4, 3));
                        if (next == (previous + 1))
                        {
                            ret = true;
                        }
                    }
                    else
                    {
                        int previous = 0;
                        int next = 0;
                        previous = Int32.Parse(previous_serial.Substring(previous_serial.Length - 3, 3));
                        next = Int32.Parse(nex_serial.Substring(nex_serial.Length - 3, 3));                        

                        if (next == (previous + 1))
                        {                            
                            ret = true;
                        }                        
                    }
                }                
            }            
            return ret;
        }
    }
}
